package com.koron.standard.patrol.controller;

import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.common.core.business.common.service.TOrderNoMaxService;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.excel.SameRowMargeStrategy;
import com.koron.standard.patrol.bean.dto.PatrolDTO;
import com.koron.standard.patrol.bean.excel.PatrolExcel;
import com.koron.standard.patrol.bean.query.PatrolQuery;
import com.koron.standard.patrol.bean.vo.PatrolVO;
import com.koron.standard.patrol.excel.PatrolExcelListener;
import com.koron.standard.patrol.excel.YCPatrolExcelListener;
import com.koron.standard.patrol.service.PatrolCheckService;
import com.koron.standard.patrol.service.PatrolRecordService;
import com.koron.standard.patrol.service.PatrolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 巡视标准(Patrol)表控制层
 *
 * @author jzp
 * @since 2021-10-19
 */
@Api(tags = "巡视标准接口")
@RestController
@RequestMapping("patrol")
public class PatrolController {

    @Autowired
    private PatrolService patrolService;

    @Autowired
    PatrolCheckService patrolCheckService;

    @Autowired
    PatrolRecordService patrolRecordService;

    @Autowired
    TOrderNoMaxService tOrderNoMaxService;


    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<Page<PatrolVO>> page(PageQuery<PatrolVO> pageQuery, PatrolQuery query) {
        Page<PatrolVO> page = patrolService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<PatrolDTO> select(@RequestParam String id) {
        PatrolDTO patrolDTO = patrolService.getDtoById(id);
        return Response.ok(patrolDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody PatrolDTO patrolDTO) {
        patrolService.saveDto(patrolDTO);
        return Response.ok(patrolDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody PatrolDTO patrolDTO) {
        patrolService.updateDtoById(patrolDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        patrolService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        patrolService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        patrolService.updateToPublic(ids);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/patrol_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("巡视标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), PatrolExcel.class, new PatrolExcelListener(patrolService, patrolCheckService, patrolRecordService, tOrderNoMaxService))
                .sheet("巡视标准")
                .headRowNumber(2)
                .doRead();

        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "YC导入")
    @PostMapping("importYC")
    public Response importFileYC(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), PatrolExcel.class, new YCPatrolExcelListener(patrolService, patrolCheckService, patrolRecordService, tOrderNoMaxService))
                .sheet("巡视标准")
                .headRowNumber(2)
                .doRead();

        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出查询出的所有数据")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, PatrolQuery query) {
        String path = "excel/patrol_standard_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("巡视标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<PatrolExcel> typeExcelList = patrolService.listPatrolExcel(query);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .sheet("巡视标准")
                .registerWriteHandler(new SameRowMargeStrategy<PatrolExcel>(typeExcelList, new HashSet<>(Arrays.asList(0, 1, 2)), PatrolExcel::getTypeId))
                .doFill(typeExcelList);
    }

}
